package view;

import javax.swing.JPanel;

import model.kwalificatiebeheer.Kerntaak;

import java.awt.GridBagLayout;

import javax.swing.JLabel;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import controller.KerntaakPanelActions;
import controller.WerkprocesTableActions;
import javax.swing.UIManager;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class KerntaakPanel extends JPanel implements Observer {
	private Kerntaak kt;
	private JButton btnSave;
	private JTextField titleField;
	private JTable wpList;
	private GridBagConstraints gbc_titleField;
	private GridBagConstraints gbc_lblPlaceholder;
	
	public KerntaakPanel(Kerntaak kt) {
		this.kt = kt;
		kt.addObserver(this);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{30, 0, 0, 0, 0, 30, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 30, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 1.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		JLabel lblTitel = new JLabel("Titel:");
		GridBagConstraints gbc_lblTitel = new GridBagConstraints();
		gbc_lblTitel.anchor = GridBagConstraints.EAST;
		gbc_lblTitel.insets = new Insets(0, 0, 5, 5);
		gbc_lblTitel.gridx = 1;
		gbc_lblTitel.gridy = 1;
		add(lblTitel, gbc_lblTitel);
			
		titleField = new JTextField();
		gbc_titleField = new GridBagConstraints();
		gbc_titleField.insets = new Insets(0, 0, 5, 5);
		gbc_titleField.fill = GridBagConstraints.HORIZONTAL;
		gbc_titleField.gridx = 2;
		gbc_titleField.gridy = 1;
		titleField.setText(kt.getTitle());
		titleField.setColumns(10);
		add(titleField, gbc_titleField);

		wpList = new JTable(new WerkprocesTableModel(kt.getUitstroomWerkprocessen()));
		wpList.setBorder(UIManager.getBorder("Table.scrollPaneBorder"));
		wpList.setRowSelectionAllowed(false);
		wpList.setCellSelectionEnabled(true);
		wpList.addMouseListener(new WerkprocesTableActions(this, wpList));
		wpList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		GridBagConstraints gbc_wpList = new GridBagConstraints();
		gbc_wpList.insets = new Insets(0, 0, 0, 0);
		gbc_wpList.gridwidth = 4;
		gbc_wpList.fill = GridBagConstraints.BOTH;
		gbc_wpList.gridx = 1;
		gbc_wpList.gridy = 5;
		add(new JScrollPane(wpList), gbc_wpList);

		JTableHeader wpHeader = wpList.getTableHeader();
		wpHeader.setReorderingAllowed(false);
		wpHeader.setBorder(UIManager.getBorder("Table.scrollPaneBorder"));
		gbc_wpList.gridy = 3;
		add(wpHeader, gbc_wpList);

		
		JButton btnVerwijderen = new JButton("verwijderen");
		btnVerwijderen.addActionListener(new KerntaakPanelActions(this));		
		btnVerwijderen.setActionCommand("btnDelete");
		GridBagConstraints gbc_btnVerwijderen = new GridBagConstraints();
		gbc_btnVerwijderen.anchor = GridBagConstraints.WEST;
		gbc_btnVerwijderen.gridwidth = 2;
		gbc_btnVerwijderen.insets = new Insets(0, 0, 5, 5);
		gbc_btnVerwijderen.gridx = 1;
		gbc_btnVerwijderen.gridy = 6;
		add(btnVerwijderen, gbc_btnVerwijderen);
		
		JButton btnWerkprocesToevoegen = new JButton("Werkproces Toevoegen");
		btnWerkprocesToevoegen.addActionListener(new KerntaakPanelActions(this));		
		btnWerkprocesToevoegen.setActionCommand("btnAddWp");
		GridBagConstraints gbc_btnWerkprocesToevoegen = new GridBagConstraints();
		gbc_btnWerkprocesToevoegen.insets = new Insets(0, 0, 5, 5);
		gbc_btnWerkprocesToevoegen.anchor = GridBagConstraints.EAST;
		gbc_btnWerkprocesToevoegen.gridx = 4;
		gbc_btnWerkprocesToevoegen.gridy = 6;
		add(btnWerkprocesToevoegen, gbc_btnWerkprocesToevoegen);
		
		btnSave = new JButton("Opslaan");
		btnSave.setActionCommand("save");
		btnSave.addActionListener(new KerntaakPanelActions(this));
		GridBagConstraints gbc_btnSave = new GridBagConstraints();
		gbc_btnSave.anchor = GridBagConstraints.EAST;
		gbc_btnSave.insets = new Insets(0, 0, 5, 5);
		gbc_btnSave.gridx = 4;
		gbc_btnSave.gridy = 7;
		add(btnSave, gbc_btnSave);
	}

	public void update(Observable arg0, Object arg1) {
		wpList.setModel(new WerkprocesTableModel(kt.getUitstroomWerkprocessen()));
		revalidate();
	}
	
	public Kerntaak getKerntaak() {
		return kt;
	}
	
	public JButton getSwitchButton() {
		return btnSave;
	}
	
	public GridBagConstraints getPlaceholderConstraints() {
		return gbc_lblPlaceholder;
	}
	
	public GridBagConstraints getTextFieldConstraints() {
		return gbc_titleField;
	}
	
	public JTextField getTextField() {
		return titleField;
	}
	
	public JTable getWpList() {
		return wpList;
	}

	public void setWpList(JTable wpList) {
		this.wpList = wpList;
	}
}